<template>
  <div>
    <el-button type="primary" style="margin: 5px 0 25px" @click="addTradeMark"
      >+添加</el-button
    >
    <div>
      <el-table style="width: 100%" :data="List" border>
        <el-table-column prop="id" label="序号" width="80" align="center">
        </el-table-column>
        <el-table-column prop="tmName" label="品牌名称"> </el-table-column>
        <el-table-column prop="address" label="品牌LOGO">
          <template slot-scope="{ row, $index }">
            <img
              src="@/assets/404_images/bc.jpg"
              style="width: 100px; height: 100px"
            />
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="{ row, $index }">
            <el-button type="warning" @click="updateTradeMark(row)"
              >修改</el-button
            >
            <el-button type="danger" @click="removeTrademark(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="getList"
      style="margintop: 50px"
      align="center"
      :current-page="page"
      :page-sizes="[3, 5, 7, 9]"
      :page-size="limit"
      layout=" prev, pager, next, jumper,->,total, sizes"
      :total="tatal"
    >
    </el-pagination>
    <!-- 对话框 -->
    <el-dialog
      :title="tmForm.id ? '修改品牌' : '添加品牌'"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="tmForm" :rules="rules" ref="rulesForm">
        <el-form-item
          label="品牌名称"
          :label-width="formLabelWidth"
          prop="tmName"
        >
          <el-input autocomplete="off" v-model="tmForm.tmName"></el-input>
        </el-form-item>
        <el-form-item
          label="品牌LOGO"
          :label-width="formLabelWidth"
          prop="logoUrl"
        >
          <!-- 上传图片 -->
          <el-upload
            class="avatar-uploader"
            action="dev-api/admin/product/fileUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="tmForm.logoUrl" :src="tmForm.logoUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrupdateTradeMark"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "TradeMark",
  data() {
    return {
      currentPage1: 5,
      currentPage2: 5,
      currentPage3: 5,
      currentPage4: 4,
      page: 1,
      limit: 3,
      List: [],
      tatal: 0,
      dialogFormVisible: false,
      formLabelWidth: "120px",
      imageUrl: "",
      tmForm: {
        tmName: "",
        logoUrl: "",
      },
      rules: {
        // rules名字不要乱写，要在tmForm中有的，并在模板中用rules，prop
        tmName: [
          { required: true, message: "请输入品牌名称", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "change" },
        ],
        logoUrl: [
          { required: true, message: "请选择品牌图片", trigger: "change" },
        ]
      },
      
    };
     
  },
  mounted() {
    this.getList();
  },
  methods: {
    handleSizeChange(val) {
      this.limit = val;
      this.getList();
    },
    async getList(pager = 1) {
      this.page = pager;
      const { page, limit } = this;
      const result = await this.$API.trademark.getTradeMark(page, limit);
      if (result.code == 200) {
        this.List = result.data.records;
        this.tatal = result.data.total;
      }
    },
    addTradeMark() {
      this.dialogFormVisible = true;
      this.tmForm = { tmName: "", logoUrl: "" };
    },
    updateTradeMark(row) {
      this.dialogFormVisible = true;
      this.tmForm = { ...row };
      console.log(row);
    },
    handleAvatarSuccess(res, file) {
      this.tmForm.logoUrl = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
     addOrupdateTradeMark() {
       console.log(1111111111,this.$refs.rulesForm);
      this.$refs.rulesForm.validate(async (success) => {
        if (success) {
          console.log(1111111,success);
          this.dialogFormVisible = false;
          let result = await this.$API.trademark.addOrupdataTradeMark(
            this.tmForm
          );
          console.log(11111, result);
          if (result.code == 200) {
            this.$message({
              showClose: true,
              message: this.tmForm.id ? "修改成功" : "添加成功",
            });
            this.getList(this.tmForm.id ? this.page : 1);
          }
        }else {
            console.log('error submit!!');
            return false;
          }
      });
    },
    async removeTrademark(id){
      console.log("woyschul1",id)
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
           let result = await this.$API.trademark.removeTrademark(id)
           if(result.code==200){
             this.$message({
            type: 'success',
            message: '删除成功!'
          });
           this.getList(this.List.length>1?this.page:this.page-1)
           }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      }
  },
};
</script>

<style>
</style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
